package com.util.security.Display;

/**
 * @ClassName: Decrypt
 * @Author: commander
 * @Description: 解密
 * @Version: 1.0
 * @Date: 2016/12/18 18:25
 */
public class Decrypt {
	private String cipher, key, otext;
	private int cipherl, keyl;
	private char[] ch1, ch3, ch4;
	private char[][] ch2;
	private int ch2r;
	private int[] in;

	public Decrypt(String cipher, String key) {
		this.cipher = cipher;
		this.key = key;
		cipherl = cipher.length();
		keyl = key.length();
		ch1 = cipher.toCharArray();
		if (cipherl % keyl == 0) {//设置二维数组行大小
			ch2r = cipherl / keyl;
		} else {
			ch2r = cipherl / keyl + 1;
		}
		in = new int[keyl];
		Keys x = new Keys(key);
		in = x.getkey();
		ch2 = new char[ch2r][keyl];
		for (int i = 0, k = 0; i < keyl; i++) {
			for (int j = 0; j < ch2r && k < cipherl; j++) {
				ch2[j][in[i]] = ch1[k];
				k++;
			}
		}
		ch4 = new char[ch2r * keyl];
		for (int i = 0, k = 0; i < ch2r; i++) {
			for (int j = 0; j < keyl && k < (ch2r * keyl); j++) {
				ch4[k] = ch2[i][j];
				k++;
			}
		}
		otext = String.valueOf(ch4);
	}

	public String gettext() {
		return otext;
	}
}
